﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MasMembers.DAL;


namespace MasMembers
{
    
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Hide();
            UserForm form = new UserForm();
            form.Show();
            form.loginForm = this;

        }
        
        private void login ()
        {
            UserEntity entity = fillEntityFromForm();
            if (entity != null)
            {
                entity = User.GetUserByName(entity.UName);
                if (entity == null)
                {
                    lblerror.Text = "خطـأ .. !! هذا المستخدم غير موجود";
                }
                else
                {
                    if (entity.UPassword == txtPassword.Text)
                    {
                        DeskTopForm form = new DeskTopForm();
                        form.currentUser = entity;
                        form.loginForm = this;
                        form.Show();
                        this.Hide();
                    }
                    else
                    {
                        lblerror.Text = "خطـأ .. !! كلمة المرور غير صحيحة";
                    }
                }
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            login();
        }

        private UserEntity fillEntityFromForm()
        {
            if (txtPassword.Text == "" || txtName.Text == "")
            {
                lblerror.Text = "خطـأ .. !!  لابد من أدخال الاسم وكلمة المرور  ";
                return null;
            }
            else
            {
                UserEntity entity = new UserEntity();
                entity.UName = txtName.Text;
                entity.UPassword = txtPassword.Text;
                return entity;
            }
        }

       public void clearFields()
        {
            this.txtName.Text = "";
            this.txtPassword.Text = "";
            this.lblerror.Text = "";
        }

       private void LoginForm_Load(object sender, EventArgs e)
       {

       }

       private void txtPassword_KeyDown(object sender, KeyEventArgs e)
       {
           if (e.KeyCode == Keys.Enter)
           {
               login();
           }
       }

       private void button1_Click_1(object sender, EventArgs e)
       {
           login();
       }

       private void linkLabel1_LinkClicked_2(object sender, LinkLabelLinkClickedEventArgs e)
       {
           UserEntity entity = fillEntityFromForm();
           if (entity != null)
           {
               entity = User.GetUserByName(entity.UName);
               if (entity == null)
               {
                   lblerror.Text = "خطـأ .. !! هذا المستخدم غير موجود";
               }
               else
               {
                   if (entity.UPassword == txtPassword.Text)
                   {
                       UserForm form = new UserForm();
                       form.currentUser = entity.Id;
                       form.loginForm = this;
                       form.Show();

                   }
               }
           }
       }
    }

}
